package com.huan.ddl;


import com.huan.Call;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;

import java.io.IOException;

//创建表
public class CreateTableAPI {


    //建表方法区
    public static void createTable(String tableName ,String... cfs) throws IOException {

        //1.判断是否存在列祖信息
        if(cfs.length < 0){
            System.out.println("请设置列祖信息");
            return;
        }

        //2.判断表是否存在

        if (NewTestAPI.isTableExist( tableName )){
            System.out.println(tableName + "表已存在");
        }

        //3.创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor( TableName.valueOf( tableName ) );

        //4.循环遍历列祖
        for (String cf : cfs) {
            //5.创建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor( cf );

            //6.添加列族
            hTableDescriptor.addFamily( hColumnDescriptor);
        }
        //7.创建表
        Admin admin = Call.admin;
        admin.createTable( hTableDescriptor );
    }

    public static void main(String[] args) throws IOException {

        //测试表是否存在
        boolean huan = NewTestAPI.isTableExist( "huan" );
        System.out.println(huan);


        //创建表
        createTable( "huan:stu","info1","info2" );
//
//
//        //再次测试
//        System.out.println(huan);


        Call.close();
    }



}
